package etude.collecteur.impl;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.io.CharArrayWriter;
import java.io.IOException;

import org.junit.Test;

import etude.collecteur.Statistic;
import etude.collecteur.Writer;

public class WriterFileTest {
	
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	
	@Test
	public void testEcrireStatisticArrayEmpty() {
		// Fixture
		Statistic[] stat = new Statistic[0];
		CharArrayWriter charWriter = new CharArrayWriter();
		
		Writer underTest = new TestableWriterFile(charWriter);
		
		// Test
		underTest.ecrire(stat);
		
		// Assert
		char[] result = charWriter.toCharArray();
		assertEquals("Date , Index, Current Size,ConnexionsUsed,MaxConnexionsUsed,MaxConnexions,AverageWaitingConnexion,AverageMaxWaitingConnexion".length()+LINE_SEPARATOR.length(), result.length);
		assertEquals("Date , Index, Current Size,ConnexionsUsed,MaxConnexionsUsed,MaxConnexions,AverageWaitingConnexion,AverageMaxWaitingConnexion"+LINE_SEPARATOR, new String(result));
	}
	
	@Test
	public void testEcrireStatisticArrayEmpty2Calls() {
		// Fixture
		Statistic[] stat = new Statistic[0];
		CharArrayWriter charWriter = new CharArrayWriter();
		
		Writer underTest = new TestableWriterFile(charWriter);
		
		// Test
		underTest.ecrire(stat);
		underTest.ecrire(stat);
		
		// Assert
		char[] result = charWriter.toCharArray();
		assertEquals("Date , Index, Current Size,ConnexionsUsed,MaxConnexionsUsed,MaxConnexions,AverageWaitingConnexion,AverageMaxWaitingConnexion".length()+LINE_SEPARATOR.length(), result.length);
		assertEquals("Date , Index, Current Size,ConnexionsUsed,MaxConnexionsUsed,MaxConnexions,AverageWaitingConnexion,AverageMaxWaitingConnexion"+LINE_SEPARATOR, new String(result));
	}
	
	@Test
	public void testEcrireStatisticArray1Stat() {
		// Fixture
		Statistic[] stat = new Statistic[1];
		stat[0] = new Statistic();
		
		CharArrayWriter charWriter = new CharArrayWriter();
		
		Writer underTest = new TestableWriterFile(charWriter);
		
		// Test
		underTest.ecrire(stat);
		
		// Assert
		char[] result = charWriter.toCharArray();
		String ligneResultat = "null,0,0,0,0,0,0,0";
		assertEquals("Date , Index, Current Size,ConnexionsUsed,MaxConnexionsUsed,MaxConnexions,AverageWaitingConnexion,AverageMaxWaitingConnexion".length()+LINE_SEPARATOR.length()+ligneResultat.length()+LINE_SEPARATOR.length(), result.length);
		assertEquals("Date , Index, Current Size,ConnexionsUsed,MaxConnexionsUsed,MaxConnexions,AverageWaitingConnexion,AverageMaxWaitingConnexion"+LINE_SEPARATOR+ligneResultat+LINE_SEPARATOR, new String(result));
	}
	
	@Test
	public void testEcrireStatisticArrayWriterCloseException() throws IOException {
		// Fixture
		Statistic[] stat = new Statistic[1];
		stat[0] = new Statistic();
		
		// Bouchon sur le Writer qui plante en cas d'appel à close()
		java.io.Writer charWriter = mock(java.io.Writer.class);
		doThrow(new IOException("testClose")).when(charWriter).close();
		
		Writer underTest = new TestableWriterFile(charWriter);
		
		// Test
		underTest.ecrire(stat);
		
		// Assert
		// On vérifie que close() a été appelé une fois
		verify(charWriter).close();
	}
	
	private static class TestableWriterFile extends WriterFile {
		private java.io.Writer writer;
		public TestableWriterFile(java.io.Writer charWriter) {
			super(null);
			writer = charWriter;
		}
		
		@Override
		protected java.io.Writer getWriter() throws IOException {
			return writer;
		}
	}

}
